﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using WMS.FC.Domain.PersistenceObject.System;
using WMS.FC.Infrastructure.Common;

namespace WMS.FC.Infrastructure.POConfig.System
{
    public class MenuConfig : POBaseConfig<MenuPO>
    {
        public override void InitializeData(EntityTypeBuilder<MenuPO> builder)
        {
            var parentId = SnowflakeIdHelper.Instance.NextId();
            builder.HasData(new MenuPO()
            {
                Id = parentId,
                NameCN = "系统管理",
                NameEN = "System Management",
                Icon = "system.png",
                TypeCode = "MenuType_Menu",
                MenuPositionCode = "MenuPosition_Menu",
                SortNumber = 0,
                ParentId = null,
                Url = null,
                Sql = null,
                ConfigJson = null,
                CreateDate = DateTime.Now,
                CreatedBy = "系统自动生成",
                ModifyDate = null,
                ModifiedBy = null,
                ModifyIp = null,
                IsDeleted = false,
            });

            builder.HasData(new MenuPO()
            {
                Id = SnowflakeIdHelper.Instance.NextId(),
                NameCN = "数据字典",
                NameEN = "Dictionary Management",
                Icon = "dictionary.png",
                TypeCode = "MenuType_Menu",
                MenuPositionCode = "MenuPosition_Menu",
                SortNumber = 1,
                ParentId = parentId,
                Url = "/SystemDataPage",
                Sql = "SELECT * FROM S_DICTIONARY T ORDER BY T.TYPE_CODE, T.SORT_NUMBER",
                ConfigJson = File.ReadAllText("Config\\MenuDictionary.json"),
                CreateDate = DateTime.Now,
                CreatedBy = "系统自动生成",
                ModifyDate = null,
                ModifiedBy = null,
                ModifyIp = null,
                IsDeleted = false,
            });

            builder.HasData(new MenuPO()
            {
                Id = SnowflakeIdHelper.Instance.NextId(),
                NameCN = "菜单管理",
                NameEN = "Menu Management",
                Icon = "menu.png",
                TypeCode = "MenuType_Menu",
                MenuPositionCode = "MenuPosition_Menu",
                SortNumber = 2,
                ParentId = parentId,
                Url = "/MenuManagement",
                Sql = null,
                ConfigJson = null,
                CreateDate = DateTime.Now,
                CreatedBy = "系统自动生成",
                ModifyDate = null,
                ModifiedBy = null,
                ModifyIp = null,
                IsDeleted = false,
            });

            builder.HasData(new MenuPO()
            {
                Id = SnowflakeIdHelper.Instance.NextId(),
                NameCN = "角色管理",
                NameEN = "Role Management",
                Icon = "role.png",
                TypeCode = "MenuType_Menu",
                MenuPositionCode = "MenuPosition_Menu",
                SortNumber = 3,
                ParentId = parentId,
                Url = "/RoleManagement",
                Sql = null,
                ConfigJson = null,
                CreateDate = DateTime.Now,
                CreatedBy = "系统自动生成",
                ModifyDate = null,
                ModifiedBy = null,
                ModifyIp = null,
                IsDeleted = false,
            });

            builder.HasData(new MenuPO()
            {
                Id = SnowflakeIdHelper.Instance.NextId(),
                NameCN = "用户管理",
                NameEN = "User Management",
                Icon = "user.png",
                TypeCode = "MenuType_Menu",
                MenuPositionCode = "MenuPosition_Menu",
                SortNumber = 4,
                ParentId = parentId,
                Url = "/UserManagement",
                Sql = null,
                ConfigJson = null,
                CreateDate = DateTime.Now,
                CreatedBy = "系统自动生成",
                ModifyDate = null,
                ModifiedBy = null,
                ModifyIp = null,
                IsDeleted = false,
            });
        }

        public override void MapColumn(EntityTypeBuilder<MenuPO> builder, ref int index)
        {
            builder.HasComment("菜单表");
            builder.Property(entity => entity.NameCN).ConfigColumn("NAME_CN", "中文名称", 100, true).HasColumnOrder(index++);
            builder.Property(entity => entity.NameEN).ConfigColumn("NAME_EN", "英文名称", 100).HasColumnOrder(index++);
            builder.Property(entity => entity.Icon).ConfigColumn("ICON", "图标", 100, true).HasColumnOrder(index++);
            builder.Property(entity => entity.TypeCode).ConfigColumn("TYPE_CODE", "类型编码", 50, true).HasColumnOrder(index++);
            builder.Property(entity => entity.MenuPositionCode).ConfigColumn("MENU_POSITION_CODE", "菜单位置编码", 50, true).HasColumnOrder(index++);
            builder.Property(entity => entity.SortNumber).ConfigColumn("SORT_NUMBER", "排序").HasColumnOrder(index++);
            builder.Property(entity => entity.ParentId).ConfigColumn("PARENT_ID", "父菜单ID").HasColumnOrder(index++);
            builder.Property(entity => entity.Url).ConfigColumn("URL", "菜单地址", 2000).HasColumnOrder(index++);
            builder.Property(entity => entity.Sql).ConfigColumn("SQL", "SQL", 4000).HasColumnOrder(index++);
            builder.Property(entity => entity.ConfigJson).ConfigColumn("CONFIG_JSON", "菜单配置", 4000).HasColumnOrder(index++);
            builder.HasOne(entity => entity.Parent).WithMany(entity => entity.SubMenu).HasForeignKey(entity => entity.ParentId).IsRequired(false).OnDelete(DeleteBehavior.Restrict);
        }

        public override void MapIndex(EntityTypeBuilder<MenuPO> builder)
        {
        }
    }
}
